#!/bin/bash
usage() {
  cat <<EOF
usage: ${0##*/} INDEX_URL
  where "INDEX_URL" is like the following:
    http://ent.sina.com.cn/s/h/2007-10-08/00041740825.shtml
Last update: Mon Oct  8 16:26:32 CST 2007
EOF
}
msg() {
  echo -e "$@" 1>&2
}
error() {
  echo -e "ERROR: $@" 1>&2
  exit -1
}
do_cmd() {
  msg "executing:\n$@";
  if "$@"; then
    msg "command success"
  else
    error "command failed:\n$@"
  fi
}

init_page=index.html
base_url=http://${base_url%%/*}
page_dir=pages
img_dir=images
if [[ -z "$1" ]]; then
  init_url=$(getclip)
else
  init_url="$1"
fi
do_cmd wget -O$init_page "$init_url"
page_title=$(grep -ihoE '<title>.*</title>' $init_page | sed -e 's/<\/\?title>//g' |
             tr -s '[:punct:][:space:]' _ | sed -e 's/_*$//')
if [[ -z "$page_title" ]]; then error "Cannot find page title."; fi
msg "Creating working directory: $page_title"
do_cmd mkdir "$page_title"
do_cmd mv $init_page "$page_title"
do_cmd cd "$page_title"
##
grep -Eoi 'http://[^"]+sina.com[^"]+/[0-9]{4}-[0-9]{2}-[0-9]{2}/[a-z0-9_-]+\.s?html' $init_page |
wget -i- -B$base_url -P$page_dir
##
grep -ihoE 'http://[^"]+/[0-9]+-[0-9]+-[0-9]+/[^"]+\.jpe?g' $page_dir/* |
grep -iv '_small_h' | sort -u |
wget -i- -P$img_dir
